﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entidades;

namespace DAO
{
    public class CinePorSalaDAO
    {
        public static void insertar(CinePorSala cineSala)
        {
            SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConexionBaseDatos"].ConnectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cn;

            string sql = "insert into CinePorSala (id_Cine, numero_Sala, capacidad) values (@idCine, @idNumSala, @capa)";
            try
            {
                cn.Open();
                cmd.CommandText = sql;
                cmd.Parameters.AddWithValue("@idCine", cineSala.Id_Cine);
                cmd.Parameters.AddWithValue("@idNumSala", cineSala.Numero_Sala);
                cmd.Parameters.AddWithValue("@capa", cineSala.Capacidad);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Error al insertar las Salas:" + ex.Message);
            }
            finally
            {
                cn.Close();
            }  
                        
        }

        public static DataTable cargarGrillaCinePorSala(int idCinSal)
        {
            SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConexionBaseDatos"].ConnectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cn;
            DataTable dt = new DataTable();
            try
            {
                cn.Open();
                cmd.CommandText = "select s.nombre, c.numero_Sala, c.capacidad from CinePorSala c, Sala s where c.numero_Sala=s.numero_Sala and c.id_cine=@id";
                cmd.Parameters.AddWithValue("@id", idCinSal);
                SqlDataReader dr = cmd.ExecuteReader();
                dt.Load(dr);
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Error al cargar la grilla:" + ex.Message);
            }
            finally
            {
                cn.Close();
            }
            return dt;
        }
    }
}